﻿using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ByteSite.WinClient.Common
{
    public static class JsonHelper
    {
        /// <summary>
        /// 只序列号需要的字段
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="arrFiled"></param>
        /// <returns></returns>
        public static string ObjectToJSONPart(object obj, string[] arrFiled)
        {
            JsonSerializerSettings jsetting = new JsonSerializerSettings();
            jsetting.ContractResolver = new LimitPropsContractResolver(arrFiled);
            return JsonConvert.SerializeObject(obj, Formatting.Indented, jsetting);
        }

        /// <summary>
        /// 转换成json格式
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public static string ObjectToJSON(object obj)
        {
            JsonSerializerSettings jsetting = new JsonSerializerSettings();
            jsetting.NullValueHandling = NullValueHandling.Ignore;
            return JsonConvert.SerializeObject(obj,jsetting);
        }

        /// <summary>
        /// json格式字符串转化成T类型对象
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="json">json字符串</param>
        /// <returns></returns>
        public static T JSONToObject<T>(string json)
        {
            return JsonConvert.DeserializeObject<T>(json);
        }

        /// <summary>
        /// 反序列化成json属性
        /// </summary>
        /// <param name="json">json字符串</param>
        /// <returns></returns>
        public static Newtonsoft.Json.Linq.JProperty DeserializeJson(string json)
        {
            return JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JProperty>(json);
        }

        /// <summary>
        /// 反序列化JSON到给定的匿名对象.
        /// </summary>
        /// <typeparam name="T">匿名对象类型</typeparam>
        /// <param name="json">json字符串</param>
        /// <param name="anonymousTypeObject">匿名对象</param>
        /// 调用：var objClass = JsonClass.DeserializeAnonymousType(obj.Data.ToString(), nClass[匿名对象]);
        /// <returns>匿名对象</returns>
        public static T DeserializeAnonymousType<T>(string json, T anonymousTypeObject)
        {
            T t = JsonConvert.DeserializeAnonymousType(json, anonymousTypeObject);
            return t;
        }

        public class LimitPropsContractResolver : DefaultContractResolver
        {
            string[] props = null;

            bool retain;

            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="props">传入的属性数组</param>
            /// <param name="retain">true:表示props是需要保留的字段  false：表示props是要排除的字段</param>
            public LimitPropsContractResolver(string[] props, bool retain = true)
            {
                //指定要序列化属性的清单
                this.props = props;

                this.retain = retain;
            }

            protected override IList<JsonProperty> CreateProperties(Type type,

            MemberSerialization memberSerialization)
            {
                IList<JsonProperty> list =
                base.CreateProperties(type, memberSerialization);
                //只保留清单有列出的属性
                return list.Where(p =>
                {
                    if (retain)
                    {
                        return props.Contains(p.PropertyName);
                    }
                    else
                    {
                        return !props.Contains(p.PropertyName);
                    }
                }).ToList();
            }
        }
    }
}
